#include "llk.h"
#include "lcd.h"
#include "front.h"
#include "ts.h"
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>

#define SIZE 8
int array[SIZE][SIZE] = {{0}};// 初始化数组为全0
int falge1 = 1;


extern char text1[4][56/8*56];//开始游戏
extern char text2[4][56/8*56];//退出游戏
extern char text3[2][96/8*97];//获胜
extern char text4[4][56/8*56];//再来一局
extern char text5[2][56/8*56];//返回

void Des_direct(int x1,int y1,int x2,int y2)//x,y轴直接相消
{
    int falgg1 = -1;
    int falgg2 = -1;
    int falgg3 = -1;

    if(x1 == x2 && abs(y1-y2)>1)//y轴直接相消
    {
        for(int i=1;(y1>y2?y2:y1)+i<(y1>y2?y1:y2);i++)
        {
            if(array[x1][(y1>y2?y2:y1)+i] != -1)
            {
                falgg1 = 0;
            }
        }
        if(falgg1 != 0)
        {
            falgg1 = 1;
        }
    }
    else if(x1 == x2 && abs(y1-y2)==1)
    {
        falgg1 = 1;
    }
    
    if(y1 == y2 && abs(x1-x2)>1)//x轴直接相消
    {
        for(int i=1;(x1>x2?x2:x1)+i<(x1>x2?x1:x2);i++)
        {
            if(array[(x1>x2?x2:x1)+i][y1] != -1)
            {
                falgg2 = 0;
            }
            if(falgg2 != 0)
            {
                falgg2 = 1;
            }
        }
    }
    else if(y1 == y2 && abs(x1-x2)==1)
    {
        falgg2 = 1;
    }

    if((x1==x2 && (x1==0 || x1==7)) || (y1==y2 && (y1==0 || y1==7)))
    {
        falgg3 = 1;
    }
    
    if(falgg1 == 1 || falgg2 == 1 || falgg3 == 1)        
    {
        lcd_draw_rectangle(60*x1,160+60*y1,60,60,0xbfefff);
        lcd_draw_rectangle(60*x2,160+60*y2,60,60,0xbfefff);
        array[x1][y1] = -1;
        array[x2][y2] = -1;
    } 
}

void Des_one_corner(int x1,int y1,int x2,int y2)//一折相消
{
    int falg_xy1 = -1;
    // int falg_xy2 = -1;

    int n1 = x1,m1 = y1;
    int n2 = x2,m2 = y2;
    if(n1 < n2 && m1 < m2)//左上  先走x轴再走y轴
    {
        while(n1 != n2 )
        {
            n1 = n1 + 1; 
            if(n1 == n2)
            {
                falg_xy1 = 1;
                break;
            }
            else if(array[n1][m1] != -1)
            {
                falg_xy1 = 0;
                break;
            }  
           
        }
        if(falg_xy1 == 1)
        {
            while(m1 != m2)
            {
                m1 = m1 +1;
                if(m1 == m2)
                {
                    falg_xy1 = 1;
                    break;
                }
                else if(array[n1][m1] != -1)
                {
                    falg_xy1 = 0;
                    break;
                }    
            }
        }
    }
    else if(n1 > n2 && m1 < m2)//右上
    {
        while(n1 != n2 )
        {
            n1 = n1 - 1; 
            if(n1 == n2)
            {
                falg_xy1 = 1;
                break;
            }
            else if(array[n1][m1] != -1)
            {
                falg_xy1 = 0;
                break;
            }  
        }
        if(falg_xy1 == 1)
        {
            while(m1 != m2)
            {
                m1 = m1 + 1;
                if(m1 == m2)
                {
                    falg_xy1 = 1;
                    break;
                }
                else if(array[n1][m1] != -1)
                {
                    falg_xy1 = 0;
                    break;
                }    
            }    
        }
    }
    else if(n1 < n2 && m1 > m2)//左下
    {
        while(n1 != n2 )
        {
            n1 = n1 + 1; 
            if(n1 == n2)
            {
                falg_xy1 = 1;
                break;
            }
            else if(array[n1][m1] != -1)
            {
                falg_xy1 = 0;
                break;
            }  
        }
        if(falg_xy1 == 1)
        {
            while(m1 != m2)
            {
                m1 = m1 - 1;
                if(m1 == m2)
                {
                    falg_xy1 = 1;
                    break;
                }
                else if(array[n1][m1] != -1)
                {
                    falg_xy1 = 0;
                    break;
                }    
            }    
        }
    }
    else if(n1 > n2 && m1 > m2)//右下
    {
        while(n1 != n2 )
        {
            n1 = n1 - 1; 
            if(n1 == n2)
            {
                falg_xy1 = 1;
                break;
            }
            else if(array[n1][m1] != -1)
            {
                falg_xy1 = 0;
                break;
            }  
        }
        if(falg_xy1 == 1)
        {
            while(m1 != m2)
            {
                m1 = m1 - 1;
                if(m1 == m2)
                {
                    falg_xy1 = 1;
                    break;
                }
                else if(array[n1][m1] != -1)
                {
                    falg_xy1 = 0;
                    break;
                }    
            }    
        }
    }

    if(falg_xy1 == 1)
    {
        lcd_draw_rectangle(60*x1,160+60*y1,60,60,0xbfefff);
        lcd_draw_rectangle(60*x2,160+60*y2,60,60,0xbfefff);
        array[x1][y1] = -1;
        array[x2][y2] = -1;
    }
}

void Des_one_corner_y_to_x(int x1,int y1,int x2,int y2)
{
    int falg_xy1 = -1;
    // int falg_xy2 = -1;

    int n1 = x1,m1 = y1;
    int n2 = x2,m2 = y2;
    if(n1 < n2 && m1 < m2)//左上  先走x轴再走y轴
    {
        

        while(m1 != m2)
        {
            m1 = m1 +1;
            if(m1 == m2)
            {
                falg_xy1 = 1;
                break;
            }
            else if(array[n1][m1] != -1)
            {
                falg_xy1 = 0;
                break;
            }    
        }
        if(falg_xy1 == 1)
        {
            while(n1 != n2 )
            {
                n1 = n1 + 1; 
                if(n1 == n2)
                {
                    falg_xy1 = 1;
                    break;
                }
                else if(array[n1][m1] != -1)
                {
                    falg_xy1 = 0;
                    break;
                }  
            }
        }
    }
    else if(n1 > n2 && m1 < m2)//右上
    {
        while(m1 != m2)
        {
            m1 = m1 + 1;
            if(m1 == m2)
            {
                falg_xy1 = 1;
                break;
            }
            else if(array[n1][m1] != -1)
            {
                falg_xy1 = 0;
                break;
            }    
        }
        if(falg_xy1 == 1)
        {
             while(n1 != n2 )
            {
                n1 = n1 - 1; 
                if(n1 == n2)
                {
                    falg_xy1 = 1;
                    break;
                }
                else if(array[n1][m1] != -1)
                {
                    falg_xy1 = 0;
                    break;
                }  
            }    
        }
    }
    else if(n1 < n2 && m1 > m2)//左下
    {
        while(m1 != m2)
        {
            m1 = m1 - 1;
            if(m1 == m2)
            {
                falg_xy1 = 1;
                break;
            }
            else if(array[n1][m1] != -1)
            {
                falg_xy1 = 0;
                break;
            }    
        }
        if(falg_xy1 == 1)
        {

            while(n1 != n2 )
            {
                n1 = n1 + 1; 
                if(n1 == n2)
                {
                    falg_xy1 = 1;
                    break;
                }
                else if(array[n1][m1] != -1)
                {
                    falg_xy1 = 0;
                    break;
                }  
            }   
        }
    }
    else if(n1 > n2 && m1 > m2)//右下
    {
        while(m1 != m2)
        {
            m1 = m1 - 1;
            if(m1 == m2)
            {
                falg_xy1 = 1;
                break;
            }
            else if(array[n1][m1] != -1)
            {
                falg_xy1 = 0;
                break;
            }    
        } 
        if(falg_xy1 == 1)
        {

            while(n1 != n2 )
            {
                n1 = n1 - 1; 
                if(n1 == n2)
                {
                    falg_xy1 = 1;
                    break;
                }
                else if(array[n1][m1] != -1)
                {
                    falg_xy1 = 0;
                    break;
                }  
            }   
        }
    }

    if(falg_xy1 == 1)
    {
        lcd_draw_rectangle(60*x1,160+60*y1,60,60,0xbfefff);
        lcd_draw_rectangle(60*x2,160+60*y2,60,60,0xbfefff);
        array[x1][y1] = -1;
        array[x2][y2] = -1;
    }
}

int Init_game()
{
    srand(time(NULL));
    for(int i=0;i<8;i++)//清空开始游戏又推出的值
    {
        for(int j=0;j<8;j++)
        {
            array[i][j] = 0;
        }
    }
    int count = 0;  // 统计已填充的元素个数

    srand(time(NULL));  // 设置随机数种子

    while(count < SIZE * SIZE)
    {
        int row1 = rand() % SIZE;      // 随机生成第一个数的行索引
        int col1 = rand() % SIZE;      // 随机生成第一个数的列索引
        int row2 = rand() % SIZE;      // 随机生成第二个数的行索引
        int col2 = rand() % SIZE;      // 随机生成第二个数的列索引

        if(array[row1][col1] == 0 && array[row2][col2] == 0 && (row1 != row2 || col1 != col2)) 
        {
            // 如果两个位置都还未填充，并且位置不重复
            int value = rand() % 6;      // 随机生成0-5的数字
            array[row1][col1] = value;   // 将数字填入第一个位置
            array[row2][col2] = value;   // 将数字填入第二个位置
            count += 2;                  // 更新已填充的元素个数
        }

    }

    for(int i=0;i<8;i++)//将0-5的图片写入屏幕
    {
        for(int j=0;j<8;j++)
        {
            if(array[i][j] == 0)
            {
                display_bmp("./0.bmp",0+60*i,160+60*j);
            }
            else if(array[i][j] == 1)
            {
                display_bmp("./1.bmp",0+60*i,160+60*j);
            }
            else if(array[i][j] == 2)
            {
                display_bmp("./2.bmp",0+60*i,160+60*j);
            }
            else if(array[i][j] == 3)
            {
                display_bmp("./3.bmp",0+60*i,160+60*j);
            }
            else if(array[i][j] == 4)
            {
                display_bmp("./4.bmp",0+60*i,160+60*j);
            }
            else if(array[i][j] == 5)
            {
                display_bmp("./5.bmp",0+60*i,160+60*j);
            }
        }
    }

    return 0;
}

struct touch_index luoji_game()
{
    int x1=-1,y1=-1,x2=-1,y2=-1;
    int a=-1,b=-1;
    struct touch_index xy1 = get_xy();
    // printf("%d %d\n",xy1.x,xy1.y);
    if(xy1.y>=0 && xy1.y<=48 && xy1.x>=752 && xy1.x<=800)
    {
        return xy1;
    }
    struct touch_index xy2 = get_xy();
    // printf("%d %d\n",xy2.x,xy2.y);
    if(xy2.y>=0 && xy2.y<=48 && xy2.x>=752 && xy2.x<=800)
    {
        return xy2;
    }
    struct touch_index xy3 = {-1,-1};

    for(int j=0;j<8;j++)
    {
        for(int i=0;i<8;i++)
        {
            if(xy1.x>=160+60*i && xy1.x<=160+60*(i+1) && xy1.y>=60*j && xy1.y<=60*(j+1))//判断第一次点击在数组几行几列
            {
                a = array[j][i];
                x1 = j;
                y1 = i;
                printf("a=%d x1=%d y1=%d\n",a,x1,y1);
            }
        }
    }
    for(int n=0;n<8;n++)
    {
        for(int m=0;m<8;m++)
        {
            if(xy2.x>=160+60*m && xy2.x<=160+60*(m+1) && xy2.y>=60*n && xy2.y<=60*(n+1))//判断第二次点击在数组几行几列
            {
                b = array[n][m];
                x2 = n;
                y2 = m;
                printf("b=%d x2=%d y2=%d\n",b,x2,y2);
            }
        }
    }
    if(a == b && (x1 != x2 || y1 != y2))//消除相同的图案
    {
        // lcd_draw_rectangle(60*x1,160+60*y1,60,60,0xbfefff);
        // lcd_draw_rectangle(60*x2,160+60*y2,60,60,0xbfefff);
        // array[x1][y1] = -1;
        // array[x2][y2] = -1;
        Des_direct(x1,y1,x2,y2);
        Des_one_corner(x1,y1,x2,y2);
        Des_one_corner_y_to_x(x1,y1,x2,y2);
    }

    return xy3;
}

int if_win()//判断是否获胜，获胜则弹出返回和再来一局
{
    int falg = 1;
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            if(array[i][j] != -1)
            {
                falg = 0;
                break;
            }
        }
    }
    if(falg == 1)
    {
        for(int i=0;i<4;i++)//写字   
        {
            draw_word(304,150+i*56,56,56,0xffff00,text4[i]);//再来一局
        }
        for(int i=0;i<2;i++)//写字  
        {
            draw_word(120,288+i*96,97,96,0xffff00,text3[i]);//获胜
            draw_word(304,482+i*56,56,56,0xffff00,text5[i]);//返回
        }
        
        struct touch_index xy4 = get_xy();
        if(xy4.y>=304 && xy4.y<=360 && xy4.x>=150 && xy4.x<=374)//点击再来一局
        {
            falge1 = 1;
            return 0;
        }
        else if(xy4.y>=304 && xy4.y<=360 && xy4.x>=538 && xy4.x<=650)//点击返回
        {
            return 1;
        }
        else 
        {
            return 0;
        }
    }
    else
    {
        return 0;
    }
    

}

void Init_UI()
{
    while(1)
    {
        Init_lcd();
        display_bmp("./background.bmp",0,0);//初始界面

        for(int i=0;i<4;i++)//写字
        {
            draw_word(96,288+i*56,56,56,0xffff00,text1[i]);//开始游戏
            draw_word(270,288+i*56,56,56,0xffff00,text2[i]);//退出游戏
        }

        struct touch_index xy = get_xy();//记录点击坐标
        if(xy.y>=96 && xy.y<=152 && xy.x>=288 && xy.x<=512)//点击开始游戏
        {
            while(1)
            {
                if(falge1 == 1)
                {
                    // for(int i = 0;i<480;i++)
                    // {
                    //     for(int j = 0;j<800;j++)
                    //     {
                    //         //画点
                    //         lcd_draw_point(i,j,0xbfefff);
                    //     }
                    // }
                    display_bmp("./back.bmp",0,752);//退出按钮
                    Init_game();
                    falge1 = 0;
                }
                struct touch_index xy0 = luoji_game();
                // struct touch_index xy0 = get_xy();//记录点击坐标
                // printf("%d %d\n",xy0.x,xy0.y);
                int fan = if_win();
                if(fan == 1 || (xy0.y>=0 && xy0.y<=48 && xy0.x>=752 && xy0.x<=800))
                    break;
                
            }
            
        }
        else if(xy.y>270 && xy.y<326 && xy.x>288 && xy.x<512)//点击退出游戏
        {
            for(int i = 0;i<480;i++)
            {
                for(int j = 0;j<800;j++)
                {
                    //画点
                    lcd_draw_point(i,j,0xffffff);
                }
            }
            break;
        }
        falge1 = 1;
        unInit_lcd();
    } 
}